<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/style/admin.css" media="all">
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字：</label>
                        <div class="layui-input-block">
                            <input type="text" name="keywords" placeholder="可按体检人姓名、电话、身份证号、预约医院搜索" autocomplete="off" class="layui-input" style="width: 600px;">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="layui-card-body">

                <div style="padding-bottom: 10px;">
                    {if (buttonAuth('order/add'))}
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-order" data-type="add"><i
                            class="layui-icon">&#xe654;</i> 添加订单</button>
                    {/if}
                    <button class="layui-btn layuiadmin-btn-order" data-type="today"> 今天</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="yesterday"> 昨天</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="week"> 本周</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="lastWeek"> 上周</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="month"> 本月</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="lastMonth"> 上月</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="year"> 今年</button>
                    <button class="layui-btn layuiadmin-btn-order" data-type="lastYear"> 去年</button>
                </div>

                <table id="LAY-user-table" lay-filter="LAY-user-table"></table>
                <script type="text/html" id="zizeng">
                    {{d.LAY_TABLE_INDEX+1}}
                </script>
                <script type="text/html" id="stateTpl">
                    {{#  if(d.state == 1){ }}
                    <button class="layui-btn layui-btn-success layui-btn-xs">待处理</button>
                    {{#  } else if(d.state == 2) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">待就诊</button>
                    {{#  } else if(d.state == 3) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">待确认</button>
                    {{#  } else if(d.state == 4) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">已就诊有效</button>
                    {{#  } else if(d.state == 5) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">已就诊无效</button>
                    {{#  } else if(d.state == 6) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">未就诊</button>
                    {{#  } else { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">已取消</button>
                    {{#  } }}
                </script>
                <script type="text/html" id="table-seller-admin">
                    {if (buttonAuth('order/detail'))}
                    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"><i
                            class="layui-icon layui-icon-face-smile-b"></i>查看</a>
                    {/if}
                    {if (buttonAuth('order/edit'))}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                            class="layui-icon layui-icon-edit"></i>编辑</a>
                    {/if}
                    {if (buttonAuth('order/del'))}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                            class="layui-icon layui-icon-delete"></i>删除</a>
                    {/if}
                </script>
            </div>
        </div>
    </div>

    <script src="/static/layui/layui.all.js"></script>
    <script src="/static/common/js/layTool.js"></script>
    <script src="/static/common/js/jquery.min.js"></script>

    <script>
        layui.config({
            base: '/static/admin/'
        }).use(['table', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table,
                util = layui.util;

            var active = {
                add: function () {
                    layTool.open("{:url('order/add')}", "添加订单", '50%', '66%');
                },
                today: function () {
                    layTool.open("{:url('order/today')}", "今天订单", '90%', '90%');
                },
                yesterday: function () {
                    layTool.open("{:url('order/yesterday')}", "昨天订单", '90%', '90%');
                },
                week: function () {
                    layTool.open("{:url('order/week')}", "本周订单", '90%', '90%');
                },
                lastWeek: function () {
                    layTool.open("{:url('order/lastWeek')}", "上周订单", '90%', '90%');
                },
                month: function () {
                    layTool.open("{:url('order/month')}", "本月订单", '90%', '90%');
                },
                lastMonth: function () {
                    layTool.open("{:url('order/lastMonth')}", "上月订单", '90%', '90%');
                },
                year: function () {
                    layTool.open("{:url('order/year')}", "今年订单", '90%', '90%');
                },
                lastYear: function () {
                    layTool.open("{:url('order/lastYear')}", "去年订单", '90%', '90%');
                }
            };

            $('.layui-btn.layuiadmin-btn-order').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            // 监听搜索
            form.on('submit(LAY-user-back-search)', function (data) {
                var field = data.field;

                // 执行重载
                table.reload('LAY-user-table', {
                    where: field
                });
            });
        });

        renderTable();
        // 渲染表格
        function renderTable() {
            layTool.table("#LAY-user-table", "/admin/order/index", [
                [
                    // {type:'radio'},
                    {
                        field: 'zizeng',
                        width: 80,
                        title: '编号',
                        sort: true,
                        templet: '#zizeng'
                    },
                    {
                        field: "name",
                        title: "体检人姓名",
                        width: 150,
                    }, {
                        field: "phone",
                        title: "体检人电话",
                        width: 150,
                    }, {
                        field: "idcard",
                        title: "体检人身份证号",
                        width: 200,
                    }, {
                        field: "sex",
                        title: "性别",
                        width: 80,
                        align: 'center',
                        sort: true,
                    }, {
                        field: "diabetes",
                        title: "是否有糖尿病",
                        width: 140,
                        align: 'center',
                        sort: true,
                    }, {
                        field: "hospital",
                        title: "预约医院",
                        minWidth: 200,
                    }, {
                        field: "check_time",
                        title: "预约体检时间",
                        templet: "<div>{{layui.util.toDateString(d.check_time,'yyyy-MM-dd')}}</div>",
                        width: 150,
                        sort: true,
                    }, {
                        field: "add_time",
                        title: "提交时间",
                        width: 180,
                        sort: true,
                    }, {
                        field: "weburl",
                        title: "来源网站",
                    }, {
                        field: "message",
                        title: "病情描述",
                    }, {
                        field: "profit",
                        title: "收入",
                        width: 100,
                        align: 'center',
                        sort: true,
                    }, {
                        field: "pay",
                        title: "支出",
                        width: 100,
                        align: 'center',
                        sort: true,
                    }, {
                        field: "info",
                        title: "备注(可点击修改)",
                        event: 'setSign',
                        style: 'cursor: pointer;'
                    }, {
                        field: "state",
                        title: "当前状态",
                        templet: '#stateTpl',
                        width: 100,
                        align: 'center'
                    }, {
                        title: "操作",
                        align: "center",
                        width: 250,
                        fixed: "right",
                        toolbar: "#table-seller-admin"
                    }
                ]
            ], 20);


            layui.use(['table', 'layer'], function () {
                        let layer = layui.layer;
                        let table = layui.table;


                        table.on("tool(LAY-user-table)",
                            function (e) {
                                if ("del" === e.event) {

                                    layer.ready(function () {
                                        var index = layer.confirm('您确定要删除该订单？', {
                                            title: '友情提示',
                                            icon: 3,
                                            btn: ['确定', '取消']
                                        }, function () {

                                            $.getJSON('{:url("order/del")}', {
                                                id: e.data.id
                                            }, function (res) {

                                                if (0 == res.code) {

                                                    layer.msg(res.msg);
                                                    setTimeout(function () {
                                                        renderTable();
                                                    }, 300);
                                                } else {
                                                    layer.alert(res.msg);
                                                }
                                            });
                                        }, function () {

                                        });
                                    });
                                } else if ("edit" === e.event) {
                                    layTool.open("/admin/order/edit/id/" + e.data.id, "编辑订单", '50%', '66%');
                                } else if ("detail" === e.event) {
                                    layTool.open("/admin/order/detail/id/" + e.data.id, "查看订单", '100%', '100%');
                                } else if ("setSign" === e.event) {
                                    layer.prompt({
                                        formType: 2,
                                        title: '修改体检人为 [' + e.data.name + '] 的备注信息',
                                        value: e.data.info
                                    }, function (value, index) {
                                        layer.close(index);

                                        $.getJSON('{:url("order/editInfo")}', {
                                                id: e.data.id,
                                                info: value
                                            }, function (res) {
                                                if (0 == res.code) {
                                                    layer.msg(res.msg);
                                                    setTimeout(function () {
                                                        renderTable();
                                                    }, 300);
                                                } else {
                                                    layer.alert(res.msg);
                                                }
                                            });

                                        //同步更新表格和缓存对应的值
                                        e.update({
                                            info: value
                                        });
                                    });
                                }
                            });
                        });
                    }
    </script>
</body>

</html>